package org.monster.context.jar;

import org.monster.context.core.ApplicationClassLoader;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * jar工厂
 *
 * @author Monster
 */
public class JarFactory {

    /**
     * 日志器
     */
    private static final Logger logger = Logger.getLogger(JarFactory.class.getName());
    /**
     * 应用类加载器
     */
    private ApplicationClassLoader loader;

    /**
     * 构造方法
     *
     * @param loader 应用类加载器
     */
    public JarFactory(ApplicationClassLoader loader) {
        this.loader = loader;
    }

    /**
     * 获取 实例
     * 工厂生产classpath实例对象
     *
     * @param classpath 类路径
     * @return 对象
     * @throws ClassNotFoundException 类未找到异常
     */
    public Object getInstance(String classpath) throws ClassNotFoundException {
        Object instance = null;
        Class<?> clazz = loader.loadClass(classpath);
        try {
            instance = clazz.newInstance();
        } catch (InstantiationException | IllegalAccessException ex) {
            logger.log(Level.SEVERE, null, ex);
        }
        return instance;
    }

}
